mongodb explain 用法
参数verbose有以下3种值
-
queryPlanner(默认) MongoDB运行查询优化器对当前的查询进行评估并选择一个最佳的查询计划
-
executionStats mongoDB运行查询优化器对当前的查询进行评估并选择一个最佳的查询计划进行执行 在执行完毕后返回这个最佳执行计划执行完成时的相关统计信息
-
allPlansExecution 即按照最佳的执行计划执行以及列出统计信息 如果有多个查询计划,还会列出这些非最佳执行计划部分的统计信息
explain 的两种用法
db[
"image-distributes-to-product"
].explain("executionStats").count({
"src_tenant": "tenant-97ognp"
}).executionStats.executionTimeMillis
db[
"image-distributes-to-product"
].aggregate([
{
"$match": {
"src_tenant": "tenant-97ognp"
}
},
{
"$project": {
"_id": 1
}
},
{
"$count": "count"
}
]).explain("executionStats")
创建和删除索引
db[
"image-distributes-to-product"
].createIndex( {
"src_tenant": 1,
"deleted_at": 1
}, {unique: false, sparse: false})
db[
"image-distributes-to-product"
].dropIndex("src_tenant_1_deleted_at_1")
sparse: 是否是稀疏索引
联合索引
db[
"image-distributes-to-product"
].aggregate([
{
"$match": {
"src_tenant": "tenant-97ognp"
}
},
{
"$group": {
"_id": "$src_workspace"
}
}
]).explain("executionStats")